package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.CustomerOfferBiz;
import com.egao.oa.entity.CustomerOffer;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-04-01 15:58:14
 * @version ：1.0
 */
@Repository
public class CustomerOfferBizImpl extends BaseDao implements CustomerOfferBiz {

	@Override
    public JsonResult list(PageResult2<T> page, CustomerOffer customerOffer, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<CustomerOffer> rm = new BeanPropertyRowMapper<CustomerOffer>(CustomerOffer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.customer,c.address,c.price,c.type");
		sql.append(",c.state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_customer_offer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id ");
        sql.append(" where c.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }

        if(loginUser.getUserId() != UserConstant.ADMIN_ID && loginUser.getUserId() != 5){
            sql.append(" and c.create_user=:create_user ");
            map.put("create_user", loginUser.getUserId() + "");
        }

        if(Utils.isNotEmpty(customerOffer)){
//			if(Utils.isNotEmpty(customerOffer.getName())){
//				sql.append(" and instr(c.name,:name)>0 ");
//				map.put("name", customerOffer.getName());
//			}
            if(Utils.isNotEmpty(customerOffer.getSort())&&Utils.isNotEmpty(customerOffer.getOrder())){
                sql.append(" order by c."+customerOffer.getSort()+" "+customerOffer.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<CustomerOffer> list(CustomerOffer customerOffer, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<CustomerOffer> rm = new BeanPropertyRowMapper<CustomerOffer>(CustomerOffer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.customer,c.address,c.price,c.type");
		sql.append(",c.state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_customer_offer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id ");
        sql.append(" where c.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(customerOffer)){
//			if(Utils.isNotEmpty(customerOffer.getName())){
//				sql.append(" and instr(c.name,:name)>0 ");
//				map.put("name", customerOffer.getName());
//			}
            if(Utils.isNotEmpty(customerOffer.getSort())&&Utils.isNotEmpty(customerOffer.getOrder())){
                sql.append(" order by c."+customerOffer.getSort()+" "+customerOffer.getOrder());
            } else {
                sql.append(" order by c.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public CustomerOffer getById(CustomerOffer customerOffer) {
        RowMapper<CustomerOffer> rm = new BeanPropertyRowMapper<CustomerOffer>(CustomerOffer.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.customer,c.address,c.price,c.type");
		sql.append(",c.state,c.remark,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_customer_offer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id ");
        sql.append(" where c.deleted=0 and sogzn.deleted=0 and c.id=:id ");
        map.put("id", customerOffer.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(CustomerOffer customerOffer, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_customer_offer (");
        sql.append("organization_id,customer_id,customer,address");
		sql.append(",price,type,state,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:customer,:address");
		sql.append(",:price,:type,:state,:remark");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", customerOffer.getCustomerId());
        map.put("customer", customerOffer.getCustomer());
        map.put("address", customerOffer.getAddress());
        map.put("price", customerOffer.getPrice());
        map.put("type", customerOffer.getType());
        map.put("state", customerOffer.getState());
        map.put("remark", customerOffer.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(CustomerOffer customerOffer, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_customer_offer set ");
        sql.append("customer_id=:customer_id,customer=:customer,address=:address ");
		sql.append(",price=:price,type=:type,state=:state ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("customer_id", customerOffer.getCustomerId());
        map.put("customer", customerOffer.getCustomer());
        map.put("address", customerOffer.getAddress());
        map.put("price", customerOffer.getPrice());
        map.put("type", customerOffer.getType());
        map.put("state", customerOffer.getState());
        map.put("remark", customerOffer.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", customerOffer.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(CustomerOffer customerOffer, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(customerOffer.getId().length()>0){
            for(String ID : customerOffer.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_customer_offer set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}